define([], function () {
  'use strict'
  // 是否显示
  function isShow (operationList, directiveVal) {
    const acl = $.auth.getAcl(...directiveVal)
    return operationList.every(operation => acl[operation])
  }
  /**
   *
   * 指令 v-auth
   * @param {Array} Array
   *
   * @example [模块|项目id, 子模块, 流程状态(默认ALL)]
   * include包含
   * exclude排除
   * 空为 全等
   * <el-button v-auth:include.c="[['system','ELProject','ALL']]">新增</el-button>
   * <el-button v-auth:exclude.dl="[['system','ELProject','Draft']]">下载</el-button>
   * <el-button v-auth.u.d="[[$.el,project.currentId,'ELProject','PENDING']]">修改</el-button>
   */
  return {
    name: 'auth',
    inserted (el, binding) {
      const type = {
        include: 'include',
        exclude: 'exclude'
      }
      const directiveVal = binding.value
      const directiveValType = Object.prototype.toString.call(binding.value)
      if (directiveValType !== '[object Array]') {
        return
      }
      const modifiers = binding.modifiers
      let operationList = []
      if (modifiers) {
        operationList = Object.keys(modifiers)
      }
      const argument = binding.argument
      let flag = true
      if (argument === type.include) {
        flag = !directiveVal.some(val => isShow(operationList, val))
      } else if (argument === type.exclude) {
        flag = !directiveVal.every(val => isShow(operationList, val))
      } else {
        flag = directiveVal.every(val => isShow(operationList, val))
      }
      if (!flag) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  }
})
